#ifndef __XRTCSERVER_MODULES_RTP_RTCP_RTCP_SRECEIVER_H_
#define __XRTCSERVER_MODULES_RTP_RTCP_RTCP_SRECEIVER_H_
#include <api/array_view.h>
#include <modules/rtp_rtcp/rtp_rtcp_config.h>
#include <modules/rtp_rtcp/source/rtcp_packet/common_header.h>
#include <modules/rtp_rtcp/source/rtcp_packet/dlrr.h>

namespace xrtc
{
    class RTCPReceiver
    {

    public:
        RTCPReceiver(const RtpRtcpConfig &config);
        ~RTCPReceiver();
        void IncomingRtcpPacket(const uint8_t *packet, size_t packet_length);
        void IncomingRtcpPacket(rtc::ArrayView<const uint8_t> packet);
        void SetRemoteSsrc(uint32_t ssrc);
        bool NTP(
            uint32_t *received_ntp_secs,
            uint32_t *received_ntp_fract,
            uint32_t *rtcp_arrival_time_secs,
            uint32_t *rtcp_arrival_time_fract,
            uint32_t *rtp_timestamp);

    private:
        struct PacketInformation;
        class RegisteredSsrcs
        {
        public:
            RegisteredSsrcs(const RtpRtcpConfig &config);
            bool Contains(uint32_t ssrc);

        private:
            std::vector<uint32_t> ssrcs_;
        };
        bool ParseCompoundPacket(rtc::ArrayView<const uint8_t> packet,
                                 PacketInformation *packet_information);
        void HandleSr(const webrtc::rtcp::CommonHeader &rtcp_block, PacketInformation *packet_information);
        void HandleRr(const webrtc::rtcp::CommonHeader &rtcp_block, PacketInformation *packet_information);
        void HandleNack(const webrtc::rtcp::CommonHeader &rtcp_block, PacketInformation *packet_information);
        void HandleXr(const webrtc::rtcp::CommonHeader &rtcp_block, PacketInformation *packet_information);
        void HandleXrDlrrReportBlock(uint32_t ssrc, const webrtc::rtcp::ReceiveTimeInfo &rti);

    private:
        webrtc::Clock *clock_;
        bool audio_;
        int num_skipped_packets_ = 0;
        uint32_t remote_ssrc_ = 0;
        uint32_t main_ssrc_;
        webrtc::NtpTime remote_sender_ntp_time_;
        uint32_t remote_sender_rtp_time_ = 0;
        webrtc::NtpTime last_received_sr_ntp_;
        uint32_t remote_sender_packet_count_ = 0;
        uint32_t remote_sender_octet_count_ = 0;
        RtpRtcpModuleObserver *rtp_rtcp_module_observer_;
        bool enable_xr_ = false;
        int64_t xr_rr_rtt_ms_ = -1;
        RegisteredSsrcs registered_ssrcs_;
    };

} // namespace xrtc
#endif